package ar.edu.itba.pod.legajo45129;

import java.net.ConnectException;
import java.rmi.RemoteException;

import org.apache.log4j.Logger;

import ar.edu.itba.pod.legajo45129.simul.communication.ConnectionManagerImpl;
import ar.edu.itba.pod.simul.ObjectFactoryAlternative;
import ar.edu.itba.pod.simul.communication.ConnectionManager;
import ar.edu.itba.pod.simul.market.MarketManager;
import ar.edu.itba.pod.simul.simulation.SimulationManager;
import ar.edu.itba.pod.simul.time.TimeMapper;

public class MyObjectFactoryAlternative implements ObjectFactoryAlternative {
	private ConnectionManagerImpl connectionManager;
//	private MarketManager marketManager;
	
	private static Logger logger = Logger.getLogger(MyObjectFactoryAlternative.class);
	
	
	public MyObjectFactoryAlternative(String host, TimeMapper timeMapper) {
		try {
			connectionManager = new ConnectionManagerImpl(host, timeMapper);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("creating object factory");
			throw new RuntimeException(e.getMessage());
		} 
//		marketManager = new MarketManagerImpl(connectionManager);
	}

	@Override
	public void connectToGroup(String entryPointId) throws ConnectException {
		try {
			connectionManager.getClusterAdmimnistration().connectToGroup(entryPointId);
		} catch (RemoteException e) {
			logger.error("connectToGroup error");
			throw new ConnectException(e.getMessage());
		}

	}

	@Override
	public void createGroup() throws ConnectException {
		try {
			connectionManager.getClusterAdmimnistration().createGroup();
		} catch (RemoteException e) {
			logger.error("createGroup error");
			throw new ConnectException(e.getMessage());
		}

	}

	@Override
	public void disconnect() {
		try {
			connectionManager.getClusterAdmimnistration()
			.disconnectFromGroup(connectionManager.getNodeId().toString());
		} catch (RemoteException e) {
			logger.error("disconnect error");
			throw new RuntimeException(e.getMessage());
		}

	}

	@Override
	public ConnectionManager getConnectionManager() {
		return connectionManager;
	}

	@Override
	public MarketManager getMarketManager() {
		return connectionManager.getMarketManager();
	}

	@Override
	public SimulationManager getSimulationManager() {
		return connectionManager.getSimulationManager();
	}

}
